package com.efounder.form.model;

import com.efounder.builder.base.data.DataSetEvent;
import com.efounder.builder.base.data.DataSetListener;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.builder.base.json.JSONUtil;
import com.efounder.form.DMComponent;
import com.efounder.form.DataSetComponent;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.base.DataComponent;
import com.efounder.form.base.IComponent;
//import com.efounder.form.service.JSONFormModelLoadService;
//import com.efounder.form.service.JSONFormModelSaveService;
import com.efounder.form.builder.IScriptObject;
import com.efounder.service.IService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Javor on 16/9/26.
 */

public class ShoppingCarFormModel extends DataComponent implements IComponent, DataSetComponent, DataSetListener, IScriptObject{
    private String id;

    //数据模型
    private EFFormDataModel formDataModel = new EFFormDataModel();

    private List<DMComponent> dmCompList;

    private Map scriptObject = new HashMap();
    private Map scriptContext = new HashMap();

    public void setActiveDataSet(EFDataSet dataSet) {
        if (dataSet == null) return;
        if (dmCompList == null || dmCompList.size() == 0) return;
        for (DMComponent comp : dmCompList) {
            if (dataSet.getTableName().equals(comp.getDataSetID())) {
                comp.setDataSet(dataSet);
            }
        }
    }

    private void load() {

    }

    private void save() {
    }

    /**
     * 将模型转成Datajson
     *
     * @return
     */
    public String convert2DataJSON() {
        return JSONUtil.RowSet2JSON(formDataModel, true);
//        return JSONUtil.DataSet2JSON(formDataModel.getBillDataSet(), true);
    }

    /**
     * 数据发生变化
     */
    public void dataChanged() {
    }

    /**
     * 将json转换成模型
     *
     * @param dataJSON
     * @param extJSON
     */
    public void toModel(String dataJSON, String extJSON) {
        formDataModel.setBillDataSet(JSONUtil.JSON2DataSet(dataJSON));

        this.setActiveDataSet(formDataModel.getBillDataSet());
    }

    protected void addFormModelDataSetListener(EFFormDataModel formDataModel) {
        EFDataSet dataSet = formDataModel.getBillDataSet();
        if (dataSet != null)
            dataSet.addDataSetListener(this);
    }

    @Override
    public String getID() {
        return id;
    }

    @Override
    public void setID(String id) {
        this.id = id;
    }

    @Override
    public void creationComplete() {
//        this.loadService = new JSONFormModelLoadService();
//        this.saveService = new JSONFormModelSaveService();
        //load();
        // save();

        //增加dataSetChanged监听
        addFormModelDataSetListener(formDataModel);
        this.setActiveDataSet(formDataModel.getBillDataSet());
    }

    @Override
    public void dataSetChanged(DataSetEvent event) {
        if (dmCompList == null) return;
        for (DMComponent comp : dmCompList) {
            comp.dataSetChanged(event);
        }
    }

    @Override
    public void insertDMComponent(DMComponent dmComponent) {
        if (dmCompList == null) {
            dmCompList = new ArrayList<DMComponent>();
        }
        dmCompList.add(dmComponent);
    }

    @Override
    public void removeDMComponent(DMComponent dmComponent) {

    }

    @Override
    public List<DMComponent> getDmCompList() {
        return dmCompList;
    }

    @Override
    public void setDmCompList(List<DMComponent> value) {
        this.dmCompList = value;
    }

    @Override
    public EFDataSet getDataSet(String dataSetKey) {
        return null;
    }

    @Override
    public List<String> getDataSetKey() {
        return null;
    }

    @Override
    public List<String> getDOMetaData(String obj_id) {
        return null;
    }

    public EFFormDataModel getFormDataModel() {
        return formDataModel;
    }

    public void setFormDataModel(EFFormDataModel formDataModel) {
        this.formDataModel = formDataModel;
    }

    @Override
    public Object loadByParam(Object param, Object userObject) {
        return null;
    }

    @Override
    public Map getScriptObject() {
        return scriptObject;
    }

    @Override
    public void setScriptObject(Map scriptObject) {
        this.scriptObject = scriptObject;
    }

    @Override
    public String getEventScript(String key) {
        return (String) scriptObject.get(key);
    }

    @Override
    public void setEventScript(String key, String script) {
        scriptObject.put(key, script);
    }

    @Override
    public Map getScriptContext() {
        return scriptContext;
    }

    @Override
    public void setScriptContext(Map context) {
        scriptContext = context;
    }
}
